package com.java.dao;

import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import com.java.entity.Order;
import com.java.utils.SqlCons;
import com.java.utils.UserSession;

/**
* @ClassName: OrderDao
* @Description: 预约数据操作类
* @author ChenJiaLu
* @date 2023年12月29日 上午11:06:10
*
 */
public class OrderDao extends BaseDao {

	public UserSession session = UserSession.getInstance();

	public List<Order> getDoctorOrders(){
		List<Order> list = new ArrayList<Order>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.ORDER.SELECT_DOCTOR);
			statement.setInt(1, session.getUserId());
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Order info = new Order();
				info.setId(res.getInt("id"));
				info.setDoctorid(res.getInt("doctorid"));
				info.setPatientid(res.getInt("patientid"));
				info.setTimeslot(res.getString("timeslot"));
				
				PreparedStatement doctorSt = getConnection().prepareStatement(SqlCons.DOCTOR.SELECT_INFO);
				doctorSt.setInt(1, res.getInt("doctorid"));
				ResultSet doctorRes = doctorSt.executeQuery();
				if(doctorRes.next()) {
					info.setDoctorName(doctorRes.getString("name"));
				}
				PreparedStatement patientSt = getConnection().prepareStatement(SqlCons.PATIENT.SELECT_INFO);
				patientSt.setInt(1, res.getInt("patientid"));
				ResultSet patientRes = patientSt.executeQuery();
				if(patientRes.next()) {
					info.setPatientName(patientRes.getString("name"));
				}
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public List<Order> getPatientOrders(){
		List<Order> list = new ArrayList<Order>();
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.ORDER.SELECT_PATIENT);
			statement.setInt(1, session.getUserId());
			ResultSet res = statement.executeQuery();
			while(res.next()) {
				Order info = new Order();
				info.setId(res.getInt("id"));
				info.setDoctorid(res.getInt("doctorid"));
				info.setPatientid(res.getInt("patientid"));
				info.setTimeslot(res.getString("timeslot"));
				
				PreparedStatement doctorSt = getConnection().prepareStatement(SqlCons.DOCTOR.SELECT_INFO);
				doctorSt.setInt(1, res.getInt("doctorid"));
				ResultSet doctorRes = doctorSt.executeQuery();
				if(doctorRes.next()) {
					info.setDoctorName(doctorRes.getString("name"));
				}
				PreparedStatement patientSt = getConnection().prepareStatement(SqlCons.PATIENT.SELECT_INFO);
				patientSt.setInt(1, res.getInt("patientid"));
				ResultSet patientRes = patientSt.executeQuery();
				if(patientRes.next()) {
					info.setPatientName(patientRes.getString("name"));
				}
				list.add(info);
			}
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
		return list;
	}

	public void delOrder(int id) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.ORDER.DELETE);
			statement.setInt(1, id);
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void saveOrder(Order order) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.ORDER.INSERT);
			statement.setInt(1, order.getDoctorid());
			statement.setInt(2, order.getPatientid());
			statement.setString(3, order.getTimeslot());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}

	public void updateOrder(Order order) {
		try {
			PreparedStatement statement = getConnection().prepareStatement(SqlCons.ORDER.UPDATE);
			statement.setInt(1, order.getDoctorid());
			statement.setInt(2, order.getPatientid());
			statement.setString(3, order.getTimeslot());
			statement.setInt(4, order.getId());
			statement.executeUpdate();
		} catch (Exception e) {
			System.err.println(e.getMessage());
		} finally {
			close();
		}
	}
}
